package com.hmdp.utils;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

@Component
public class RedisIdWorker {
    private static final Long BEGIN_TIMEMP = 1640995200L;
    private static final int CONT_BASE = 32;
    private final StringRedisTemplate stringRedisTemplate;

    public RedisIdWorker(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    public long nextId(String keyPredix){
        //时间戳 + 序列号
        LocalDateTime now = LocalDateTime.now();
        long newSecond = now.toEpochSecond(ZoneOffset.UTC);
        long timestamp = newSecond- BEGIN_TIMEMP;

        String date = now.format(DateTimeFormatter.ofPattern("yyyy:MM:dd"));
        Long count = stringRedisTemplate.opsForValue().increment("icr:" + keyPredix + ":" + date);




        return timestamp << 32 | count;
    }
    public static void main(String args[]){
        LocalDateTime time = LocalDateTime.of(2022,1,1,0,0,0);
        long second =time.toEpochSecond(ZoneOffset.UTC);
        System.out.println(second);
    }
}
